﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DacSanMienTayOBJ
{
    public class ProductBusiness
    {
        public static bool InsertProduct(Product product, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                bool rs = false;
                string sql = "insert into product(AdminID,ProductName,OriginalPrice,RealPrice,Quantity,ImageProduct,MeasurementUnits,ContentProduct,ProductOriginaly,ProductGuide,Metakeyword,CreateDate,ExpireDate,Priority,IsVisible) values(@adminID,@productName,@originalPrice,@realPrice,@quantity,@imageProduct,@measurementUnits,@contentProduct,@productOriginal,@productGuide,@metakey,@createDate,@expireDate,@priority,@isVisible)";
                SqlParameter[] pa = new SqlParameter[15];
                pa[0] = new SqlParameter("@adminID", product.AdminID);
                pa[1] = new SqlParameter("@productName", product.ProductName);
                pa[2] = new SqlParameter("@originalPrice", product.OriginalPrice);
                pa[3] = new SqlParameter("@realPrice", product.RealPrice);
                pa[4] = new SqlParameter("@quantity", product.Quantity);
                pa[5] = new SqlParameter("@measurementUnits", product.MeasurementUnits);
                pa[6] = new SqlParameter("@contentProduct", product.ContentProduct);
                pa[7] = new SqlParameter("@productOriginal", product.ProductOriginal);
                pa[8] = new SqlParameter("@productGuide", product.ProductGuide);
                pa[9] = new SqlParameter("@createDate", product.CreateDate);
                pa[10] = new SqlParameter("@expireDate", product.ExpireDate);
                pa[11] = new SqlParameter("@priority", product.Priority);
                pa[12] = new SqlParameter("@isVisible", product.IsVisble);
                pa[13] = new SqlParameter("@imageProduct",product.ProductImage);
                pa[14] = new SqlParameter("@metakey",product.Metakeyword);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static bool UpdateProduct(int productID,Product product, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                bool rs = false;
                string sql = "update product set ProductName=@productName,OriginalPrice=@originalPrice,RealPrice=@realPrice,Quantity=@quantity,ImageProduct=@imageProduct,MeasurementUnits=@measurementUnits,ContentProduct=@contentProduct,ProductOriginaly=@productOriginal,ProductGuide=@productGuide,Metakeyword=@metakey where ProductID=@productID";
                SqlParameter[] pa = new SqlParameter[11];
                pa[0] = new SqlParameter("@productID", productID);
                pa[1] = new SqlParameter("@productName", product.ProductName);
                pa[2] = new SqlParameter("@originalPrice", product.OriginalPrice);
                pa[3] = new SqlParameter("@realPrice", product.RealPrice);
                pa[4] = new SqlParameter("@quantity", product.Quantity);
                pa[5] = new SqlParameter("@measurementUnits", product.MeasurementUnits);
                pa[6] = new SqlParameter("@contentProduct", product.ContentProduct);
                pa[7] = new SqlParameter("@productOriginal", product.ProductOriginal);
                pa[8] = new SqlParameter("@productGuide", product.ProductGuide);
                pa[9] = new SqlParameter("@imageProduct",product.ProductImage);
                pa[10] = new SqlParameter("@metakey", product.Metakeyword);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static bool UpdateProductVisible(int productID, int visible, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                bool rs = false;
                string sql = "update product set IsVisible=@isVisible where ProductID=@productID";
                SqlParameter[] pa = new SqlParameter[2];
                pa[0] = new SqlParameter("@isVisible",visible);
                pa[1] = new SqlParameter("@productID",productID);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static List<Product> ListAllProduct(int start, int end, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                List<Product> list = new List<Product>();
                string sql = "select *from (SELECT ROW_NUMBER() OVER (ORDER BY ProductID DESC) as Row,ProductID,AdminID,ProductName,OriginalPrice,RealPrice,Quantity,ImageProduct,MeasurementUnits,ContentProduct,ProductOriginaly,ProductGuide,Metakeyword,CreateDate,ExpireDate,Priority,IsVisible from product) as Products  where Row>=@startIndex and Row<=@endIndex";
                SqlParameter[] pa = new SqlParameter[2];
                pa[0] = new SqlParameter("@startIndex",start);
                pa[1] = new SqlParameter("@endIndex",end);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Product product = new Product();
                    product.ProductID = int.Parse(reader["ProductID"].ToString());
                    product.ProductName = reader["ProductName"].ToString();
                    product.AdminID=int.Parse(reader["AdminID"].ToString());
                    product.OriginalPrice=float.Parse(reader["OriginalPrice"].ToString());
                    product.RealPrice=float.Parse(reader["RealPrice"].ToString());
                    product.Quantity=int.Parse(reader["Quantity"].ToString());
                    product.MeasurementUnits=reader["MeasurementUnits"].ToString();
                    product.ContentProduct=reader["ContentProduct"].ToString();
                    product.ProductOriginal=reader["ProductOriginaly"].ToString();
                    product.ProductGuide=reader["ProductGuide"].ToString();
                    product.CreateDate=DateTime.Parse(reader["CreateDate"].ToString());
                    product.ExpireDate=DateTime.Parse(reader["ExpireDate"].ToString());
                    product.Priority=int.Parse(reader["Priority"].ToString());
                    product.IsVisble=int.Parse(reader["IsVisible"].ToString());
                    product.ProductImage = reader["ImageProduct"].ToString();
                    product.Metakeyword = reader["Metakeyword"].ToString();
                    list.Add(product);
                }
                return list;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return null;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static int CountAllProduct(ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                int count = 0;
                string sql = "select count(ProductID) as totalProduct from product";
               
                SqlCommand command = helper.GetCommandNonParameter(sql, false);
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    count = int.Parse(reader["totalProduct"].ToString()); 
                   
                }
                return count;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return 0;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static List<Product> ListAllProduct(int visible,int start, int end, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                List<Product> list = new List<Product>();
                string sql = "select *from (SELECT ROW_NUMBER() OVER (ORDER BY ProductID DESC) as Row,ProductID,AdminID,ProductName,OriginalPrice,RealPrice,Quantity,ImageProduct,MeasurementUnits,ContentProduct,ProductOriginaly,ProductGuide,Metakeyword,CreateDate,ExpireDate,Priority,IsVisible from product where IsVisible=@isVisible) as Products  where Row>=@startIndex and Row<=@endIndex";
                SqlParameter[] pa = new SqlParameter[3];
                pa[0] = new SqlParameter("@startIndex",start);
                pa[1] = new SqlParameter("@endIndex",end);
                pa[2]=new SqlParameter("@isVisible",visible);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Product product = new Product();
                    product.ProductID = int.Parse(reader["ProductID"].ToString());
                    product.ProductName = reader["ProductName"].ToString();
                    product.AdminID=int.Parse(reader["AdminID"].ToString());
                    product.OriginalPrice=float.Parse(reader["OriginalPrice"].ToString());
                    product.RealPrice=float.Parse(reader["RealPrice"].ToString());
                    product.Quantity=int.Parse(reader["Quantity"].ToString());
                    product.MeasurementUnits=reader["MeasurementUnits"].ToString();
                    product.ContentProduct=reader["ContentProduct"].ToString();
                    product.ProductOriginal=reader["ProductOriginaly"].ToString();
                    product.ProductGuide=reader["ProductGuide"].ToString();
                    product.CreateDate=DateTime.Parse(reader["CreateDate"].ToString());
                    product.ExpireDate=DateTime.Parse(reader["ExpireDate"].ToString());
                    product.Priority=int.Parse(reader["Priority"].ToString());
                    product.IsVisble=int.Parse(reader["IsVisible"].ToString());
                    product.ProductImage = reader["ImageProduct"].ToString();
                    product.Metakeyword = reader["Metakeyword"].ToString();
                    list.Add(product);
                }
                return list;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return null;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static List<Product> ListAllProductFollowGroup(int visible, int groupID, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                List<Product> list = new List<Product>();
                string sql = "select p.ProductID,p.AdminID,p.ProductName,p.OriginalPrice,p.RealPrice,p.Quantity,p.ImageProduct,p.MeasurementUnits,p.ContentProduct,p.ProductOriginaly,p.ProductGuide,p.Metakeyword,p.CreateDate,p.ExpireDate,p.Priority,p.IsVisible from product p,productgroup pg where p.ProductID=pg.ProductID and pg.GroupID=@groupID and p.IsVisible=@isVisible order by p.ProductID desc";
                SqlParameter[] pa = new SqlParameter[2];
                pa[0] = new SqlParameter("@groupID", groupID);
                pa[1] = new SqlParameter("@isVisible", visible);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Product product = new Product();
                    product.ProductID = int.Parse(reader["ProductID"].ToString());
                    product.ProductName = reader["ProductName"].ToString();
                    product.AdminID = int.Parse(reader["AdminID"].ToString());
                    product.OriginalPrice = float.Parse(reader["OriginalPrice"].ToString());
                    product.RealPrice = float.Parse(reader["RealPrice"].ToString());
                    product.Quantity = int.Parse(reader["Quantity"].ToString());
                    product.MeasurementUnits = reader["MeasurementUnits"].ToString();
                    product.ContentProduct = reader["ContentProduct"].ToString();
                    product.ProductOriginal = reader["ProductOriginaly"].ToString();
                    product.ProductGuide = reader["ProductGuide"].ToString();
                    product.CreateDate = DateTime.Parse(reader["CreateDate"].ToString());
                    product.ExpireDate = DateTime.Parse(reader["ExpireDate"].ToString());
                    product.Priority = int.Parse(reader["Priority"].ToString());
                    product.IsVisble = int.Parse(reader["IsVisible"].ToString());
                    product.ProductImage = reader["ImageProduct"].ToString();
                    product.Metakeyword = reader["Metakeyword"].ToString();
                    list.Add(product);
                }
                return list;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return null;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static List<Product> ListAllProductFollowCategory(int visible, int cateID,int startIndex,int endIndex, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                List<Product> list = new List<Product>();
                string sql = "select *from (SELECT ROW_NUMBER() OVER (ORDER BY p.ProductID DESC) as Row,p.ProductID,p.AdminID,p.ProductName,p.OriginalPrice,p.RealPrice,p.Quantity,p.ImageProduct,p.MeasurementUnits,p.ContentProduct,p.ProductOriginaly,p.ProductGuide,p.Metakeyword,p.CreateDate,p.ExpireDate,p.Priority,p.IsVisible from product p,categoryproduct cp where p.ProductID=cp.ProductID and cp.CategoryID=@cateID and p.IsVisible=@isVisible) as Products  where Row>=@startIndex and Row<=@endIndex";
                SqlParameter[] pa = new SqlParameter[4];
                pa[0] = new SqlParameter("@cateID", cateID);
                pa[1] = new SqlParameter("@isVisible", visible);
                pa[2] = new SqlParameter("@startindex", startIndex);
                pa[3] = new SqlParameter("@endIndex", endIndex);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Product product = new Product();
                    product.ProductID = int.Parse(reader["ProductID"].ToString());
                    product.ProductName = reader["ProductName"].ToString();
                    product.AdminID = int.Parse(reader["AdminID"].ToString());
                    product.OriginalPrice = float.Parse(reader["OriginalPrice"].ToString());
                    product.RealPrice = float.Parse(reader["RealPrice"].ToString());
                    product.Quantity = int.Parse(reader["Quantity"].ToString());
                    product.MeasurementUnits = reader["MeasurementUnits"].ToString();
                    product.ContentProduct = reader["ContentProduct"].ToString();
                    product.ProductOriginal = reader["ProductOriginaly"].ToString();
                    product.ProductGuide = reader["ProductGuide"].ToString();
                    product.CreateDate = DateTime.Parse(reader["CreateDate"].ToString());
                    product.ExpireDate = DateTime.Parse(reader["ExpireDate"].ToString());
                    product.Priority = int.Parse(reader["Priority"].ToString());
                    product.IsVisble = int.Parse(reader["IsVisible"].ToString());
                    product.ProductImage = reader["ImageProduct"].ToString();
                    product.Metakeyword = reader["Metakeyword"].ToString();
                    list.Add(product);
                }
                return list;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return null;
            }
            finally
            {
                helper.destroy();
            }
        }

         public static int CountAllProduct(int visible,ConnectionType typeConnect)
         {
             Sqlhelper helper = new Sqlhelper(typeConnect);
             try
             {
                 int count = 0;
                 string sql = "select count(ProductID) as totalProduct from product where IsVisible=@isVisible";
                 SqlParameter[] pa = new SqlParameter[1];

                 pa[0] = new SqlParameter("@isVisible", visible);
                 SqlCommand command = helper.GetCommandNonParameter(sql, false);
                 SqlDataReader reader = command.ExecuteReader();
                 if (reader.Read())
                 {
                     count = int.Parse(reader["totalProduct"].ToString());

                 }
                 return count;
             }
             catch (Exception ex)
             {
                 Utilitys.WriteLog(ex.Message);
                 return 0;
             }
             finally
             {
                 helper.destroy();
             }
         }

         public static int CountAllProductFollowCategory(int visible,int cateID, ConnectionType typeConnect)
         {
             Sqlhelper helper = new Sqlhelper(typeConnect);
             try
             {
                 int count = 0;
                 string sql = "select count(p.ProductID) as totalProduct from product p,categoryproduct cp where p.ProductID=cp.ProductID and cp.CategoryID=@cateID and IsVisible=@isVisible group by p.ProductID";
                 SqlParameter[] pa = new SqlParameter[2];

                 pa[0] = new SqlParameter("@isVisible", visible);
                 pa[1] = new SqlParameter("@cateID", cateID);
                 SqlCommand command = helper.GetCommandNonParameter(sql, false);
                 SqlDataReader reader = command.ExecuteReader();
                 if (reader.Read())
                 {
                     count = int.Parse(reader["totalProduct"].ToString());

                 }
                 return count;
             }
             catch (Exception ex)
             {
                 Utilitys.WriteLog(ex.Message);
                 return 0;
             }
             finally
             {
                 helper.destroy();
             }
         }

         public static Product GetProductDetail(int productID, ConnectionType typeConnect)
         {
             Sqlhelper helper = new Sqlhelper(typeConnect);
             try
             {
                 Product product = null;
                 string sql = "select *from product where ProductID=@productID";
                 SqlParameter[] pa = new SqlParameter[1];
                 pa[0] = new SqlParameter("@productID", productID);
                 SqlCommand command = helper.GetCommand(sql, pa, false);
                 SqlDataReader reader = command.ExecuteReader();
                 if (reader.Read())
                 {
                     product = new Product();
                     product.ProductID = int.Parse(reader["ProductID"].ToString());
                     product.ProductName = reader["ProductName"].ToString();
                     product.AdminID = int.Parse(reader["AdminID"].ToString());
                     product.OriginalPrice = float.Parse(reader["OriginalPrice"].ToString());
                     product.RealPrice = float.Parse(reader["RealPrice"].ToString());
                     product.Quantity = int.Parse(reader["Quantity"].ToString());
                     product.MeasurementUnits = reader["MeasurementUnits"].ToString();
                     product.ContentProduct = reader["ContentProduct"].ToString();
                     product.ProductOriginal = reader["ProductOriginaly"].ToString();
                     product.ProductGuide = reader["ProductGuide"].ToString();
                     product.CreateDate = DateTime.Parse(reader["CreateDate"].ToString());
                     product.ExpireDate = DateTime.Parse(reader["ExpireDate"].ToString());
                     product.Priority = int.Parse(reader["Priority"].ToString());
                     product.IsVisble = int.Parse(reader["IsVisible"].ToString());
                     product.ProductImage = reader["ImageProduct"].ToString();
                     product.Metakeyword = reader["Metakeyword"].ToString();
                 }
                 return product;
             }
             catch (Exception ex)
             {
                 Utilitys.WriteLog(ex.Message);
                 return null;
             }
             finally
             {
                 helper.destroy();
             }
         }

         public static int GetProductIDNewest( ConnectionType typeConnect)
         {
             Sqlhelper helper = new Sqlhelper(typeConnect);
             try
             {
                 int productID = 0;
                 string sql = "select top 1 ProductID from product order by ProductID desc";
                 SqlParameter[] pa = new SqlParameter[1];
                 pa[0] = new SqlParameter("@productID", productID);
                 SqlCommand command = helper.GetCommand(sql, pa, false);
                 SqlDataReader reader = command.ExecuteReader();
                 if (reader.Read())
                 {
                    
                     productID = int.Parse(reader["ProductID"].ToString());
                    
                 }
                 return productID;
             }
             catch (Exception ex)
             {
                 Utilitys.WriteLog(ex.Message);
                 return 0;
             }
             finally
             {
                 helper.destroy();
             }
         }

         public static bool InsertProductGroup(int productID, int groupID, ConnectionType typeConnect)
         {
             Sqlhelper helper = new Sqlhelper(typeConnect);
             try
             {
                 bool rs = false;
                 string sql = "insert into productgroup(ProductID,GroupID) values(@productID,@groupID)";
                 SqlParameter[] pa = new SqlParameter[2];
                 pa[0] = new SqlParameter("@productID",productID);
                 pa[1] = new SqlParameter("@groupID",groupID);
                 SqlCommand command = helper.GetCommand(sql, pa, false);
                 int row = command.ExecuteNonQuery();
                 if (row > 0)
                 {
                     rs = true;
                 }
                 return rs;
             }
             catch (Exception ex)
             {
                 Utilitys.WriteLog(ex.Message);
                 return false;
             }
             finally
             {
                 helper.destroy();
             }
         }

         public static bool DeleteProductGroupByProductID(int productID, ConnectionType typeConnect)
         {
             Sqlhelper helper = new Sqlhelper(typeConnect);
             try
             {
                 bool rs = false;
                 string sql = "delete productgroup where ProductID=@productID";
                 SqlParameter[] pa = new SqlParameter[1];
                 pa[0] = new SqlParameter("@productID", productID);
                 SqlCommand command = helper.GetCommand(sql, pa, false);
                 int row = command.ExecuteNonQuery();
                 if (row > 0)
                 {
                     rs = true;
                 }
                 return rs;
             }
             catch (Exception ex)
             {
                 Utilitys.WriteLog(ex.Message);
                 return false;
             }
             finally
             {
                 helper.destroy();
             }
         }

         public static bool DeleteProductGroupByGroupID(int groupID, ConnectionType typeConnect)
         {
             Sqlhelper helper = new Sqlhelper(typeConnect);
             try
             {
                 bool rs = false;
                 string sql = "delete productgroup where GroupID=@groupID";
                 SqlParameter[] pa = new SqlParameter[1];
                 pa[0] = new SqlParameter("@groupID", groupID);
                 SqlCommand command = helper.GetCommand(sql, pa, false);
                 int row = command.ExecuteNonQuery();
                 if (row > 0)
                 {
                     rs = true;
                 }
                 return rs;
             }
             catch (Exception ex)
             {
                 Utilitys.WriteLog(ex.Message);
                 return false;
             }
             finally
             {
                 helper.destroy();
             }
         }

         public static bool InsertProductImage(int productID, int imageID, ConnectionType typeConnect)
         {
             Sqlhelper helper = new Sqlhelper(typeConnect);
             try
             {
                 bool rs = false;
                 string sql = "insert into productimage(ProductID,ImageID) values(@productID,@imageID)";
                 SqlParameter[] pa = new SqlParameter[2];
                 pa[0] = new SqlParameter("@productID", productID);
                 pa[1] = new SqlParameter("@imageID", imageID);
                 SqlCommand command = helper.GetCommand(sql, pa, false);
                 int row = command.ExecuteNonQuery();
                 if (row > 0)
                 {
                     rs = true;
                 }
                 return rs;
             }
             catch (Exception ex)
             {
                 Utilitys.WriteLog(ex.Message);
                 return false;
             }
             finally
             {
                 helper.destroy();
             }
         }

         public static bool DeleteProductImageByProductID(int productID, ConnectionType typeConnect)
         {
             Sqlhelper helper = new Sqlhelper(typeConnect);
             try
             {
                 bool rs = false;
                 string sql = "delete productimage where ProductID=@productID";
                 SqlParameter[] pa = new SqlParameter[1];
                 pa[0] = new SqlParameter("@productID", productID);
                 SqlCommand command = helper.GetCommand(sql, pa, false);
                 int row = command.ExecuteNonQuery();
                 if (row > 0)
                 {
                     rs = true;
                 }
                 return rs;
             }
             catch (Exception ex)
             {
                 Utilitys.WriteLog(ex.Message);
                 return false;
             }
             finally
             {
                 helper.destroy();
             }
         }

         public static bool DeleteProductImageByImageID(int imageID, ConnectionType typeConnect)
         {
             Sqlhelper helper = new Sqlhelper(typeConnect);
             try
             {
                 bool rs = false;
                 string sql = "delete productimage where ImageID=@imageID";
                 SqlParameter[] pa = new SqlParameter[1];
                 pa[0] = new SqlParameter("@imageID", imageID);
                 SqlCommand command = helper.GetCommand(sql, pa, false);
                 int row = command.ExecuteNonQuery();
                 if (row > 0)
                 {
                     rs = true;
                 }
                 return rs;
             }
             catch (Exception ex)
             {
                 Utilitys.WriteLog(ex.Message);
                 return false;
             }
             finally
             {
                 helper.destroy();
             }
         }

         public static bool InsertProductDistrict(int productID, int districtID, ConnectionType typeConnect)
         {
             Sqlhelper helper = new Sqlhelper(typeConnect);
             try
             {
                 bool rs = false;
                 string sql = "insert into districtproduct(ProductID,DistrictID) values(@productID,@districtID)";
                 SqlParameter[] pa = new SqlParameter[2];
                 pa[0] = new SqlParameter("@productID", productID);
                 pa[1] = new SqlParameter("@districtID", districtID);
                 SqlCommand command = helper.GetCommand(sql, pa, false);
                 int row = command.ExecuteNonQuery();
                 if (row > 0)
                 {
                     rs = true;
                 }
                 return rs;
             }
             catch (Exception ex)
             {
                 Utilitys.WriteLog(ex.Message);
                 return false;
             }
             finally
             {
                 helper.destroy();
             }
         }

         public static bool DeleteProductDistrictByProductID(int productID, ConnectionType typeConnect)
         {
             Sqlhelper helper = new Sqlhelper(typeConnect);
             try
             {
                 bool rs = false;
                 string sql = "delete districtproduct where ProductID=@productID";
                 SqlParameter[] pa = new SqlParameter[1];
                 pa[0] = new SqlParameter("@productID", productID);
                 SqlCommand command = helper.GetCommand(sql, pa, false);
                 int row = command.ExecuteNonQuery();
                 if (row > 0)
                 {
                     rs = true;
                 }
                 return rs;
             }
             catch (Exception ex)
             {
                 Utilitys.WriteLog(ex.Message);
                 return false;
             }
             finally
             {
                 helper.destroy();
             }
         }

         public static bool DeleteProductDistrictByDistrictID(int districtID, ConnectionType typeConnect)
         {
             Sqlhelper helper = new Sqlhelper(typeConnect);
             try
             {
                 bool rs = false;
                 string sql = "delete districtproduct where DistrictID=@districtID";
                 SqlParameter[] pa = new SqlParameter[1];
                 pa[0] = new SqlParameter("@districtID", districtID);
                 SqlCommand command = helper.GetCommand(sql, pa, false);
                 int row = command.ExecuteNonQuery();
                 if (row > 0)
                 {
                     rs = true;
                 }
                 return rs;
             }
             catch (Exception ex)
             {
                 Utilitys.WriteLog(ex.Message);
                 return false;
             }
             finally
             {
                 helper.destroy();
             }
         }

         public static bool InsertProductMap(int originalProductID, int destinationProductID, ConnectionType typeConnect)
         {
             Sqlhelper helper = new Sqlhelper(typeConnect);
             try
             {
                 bool rs = false;
                 string sql = "insert into productmap(ProductOriginalID,ProductDestinationID) values(@originalProductID,@destinationProductID)";
                 SqlParameter[] pa = new SqlParameter[2];
                 pa[0] = new SqlParameter("@originalProductID",originalProductID);
                 pa[1] = new SqlParameter("@destinationProductID",destinationProductID);
                 SqlCommand command = helper.GetCommand(sql, pa, false);
                 int row = command.ExecuteNonQuery();
                 if (row > 0)
                 {
                     rs = true;
                 }
                 return rs;
             }
             catch (Exception ex)
             {
                 Utilitys.WriteLog(ex.Message);
                 return false;
             }
             finally
             {
                 helper.destroy();
             }
         }

         public static bool DeleteProductMapByOrginalProduct(int originalProductID, ConnectionType typeConnect)
         {
             Sqlhelper helper = new Sqlhelper(typeConnect);
             try
             {
                 bool rs = false;
                 string sql = "delete productmap where ProductOriginalID=@originalProductID";
                 SqlParameter[] pa = new SqlParameter[1];
                 pa[0] = new SqlParameter("@originalProductID", originalProductID);
                 SqlCommand command = helper.GetCommand(sql, pa, false);
                 int row = command.ExecuteNonQuery();
                 if (row > 0)
                 {
                     rs = true;
                 }
                 return rs;
             }
             catch (Exception ex)
             {
                 Utilitys.WriteLog(ex.Message);
                 return false;
             }
             finally
             {
                 helper.destroy();
             }
         }
    }
}
